package com.sneakxy.cloudbase.platform.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.sneakxy.cloudbase.platform.bean.Home;
import com.sneakxy.cloudbase.platform.bean.dto.HomeDTO;
import com.sneakxy.cloudbase.platform.service.IHomeService;
import com.sneakxy.cloudbase.platform.utils.web.BeanHelper;
import com.sneakxy.cloudbase.platform.utils.web.Response;
import com.sneakxy.cloudbase.platform.utils.web.signature.RequireSignature;
import com.sneakxy.mybatis.commons.repository.query.domain.CriteriaBuilder;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 首页模板信息服务接口
 * @author 潜行的虚影
 * @see
 */
@Api(tags="首页模板信息服务接口")
@RestController
@RequestMapping("/api/home/")
public class HomeController extends BaseController {

	@Autowired
	private IHomeService homeService;
	
	@ApiOperation("首页模板信息列表")
	@RequireSignature
	@RequiresPermissions("security:home:manager")
	@PostMapping("query")
	public Response<List<HomeDTO>> query(
			@ApiParam(name="type", value="菜单类型", required = true)
			@RequestParam(name="type")
			String type) {
		CriteriaBuilder<Home> builder = new CriteriaBuilder<>(Home.class);
		builder.andEq("id.type", type);
		List<Home> result = homeService.findAll(builder.build());
		return Response.ok(HomeDTO.from(result));
	}
	
	@ApiOperation("首页模板信息设置")
	@RequireSignature
	@RequiresPermissions("security:home:manager:save")
	@ApiOperationSupport(ignoreParameters = {"home.createtime", "home.updatetime"})
	@PostMapping("save")
	public Response<HomeDTO> save(
			@Validated
			@RequestBody
			HomeDTO home) {
		//去空格
		BeanHelper.trimToEmpty(home);
		
		boolean hasOld =  homeService.exists(HomeDTO.toId(home));
		Home entity = HomeDTO.to(home);
		entity.setCreatetime(null);
		entity.setUpdatetime(null);
		entity.setNew(!hasOld);
		entity = homeService.save(entity);
		return Response.ok(HomeDTO.from(entity));
	}
	
	@ApiImplicitParams({
		@ApiImplicitParam(name="type", value="菜单类型", dataType="String", dataTypeClass = String.class, paramType="query", required=true),
		@ApiImplicitParam(name="login", value="是否登录:false、未登录，true、登录", dataType="boolean", dataTypeClass = Boolean.class, paramType="query", required=true)
	})
	@ApiOperation("查找一个首页模板信息")
	@RequireSignature
	@PostMapping("one")
	public Response<HomeDTO> one(
			@ApiIgnore
			@RequestParam(name="type")
			String type,
			@ApiIgnore
			@RequestParam(name="login")
			boolean login) {
		Home result = homeService.findOne(new Home.HomeId(type, login));
		return Response.ok(result, HomeDTO::from);
	}
	
	@ApiOperation("首页模板信息")
	@PostMapping("template")
	@RequestMapping(value = "template", method = {RequestMethod.GET, RequestMethod.POST})
	public Response<String> template(
			@ApiParam(name="type", value="菜单类型", required = true)
			@RequestParam(name="type")
			String type) {
		Home.HomeId id = new Home.HomeId();
		id.setType(type);
		id.setLogin(getLoginAccount() != null);
		Home home = homeService.findOne(id);
		return home != null ? Response.ok(StringUtils.EMPTY, home.getTemplate()) : Response.fail();
	}
	
}
